test: Add tests for CTX manager usage on PEP0249.#448
Conversation
Ferenc-
left a comment
There was a problem hiding this comment.
Please add a TC where the db is not managed, but the cursor is.
Please cover the rest of the instrumentations where PEP0248 is used, like mysql.
Add the test_connect_ctx_mngr UT to test the span creation when using the psycopg2 connect with context manager, and the test_connect_cursor_ctx_mngr UT when using the psycopg2 connect and cursos context manager. Signed-off-by: Paulo Vital <paulo.vital@ibm.com>
f67eabc to
ccf7ce0
Compare
|
I am closing this PR once we use #450 to track this fix. |
| self.assertEqual(test_span.t, db_span.t) | ||
| self.assertEqual(db_span.p, test_span.s) | ||
|
|
||
| self.assertEqual(None, db_span.ec) |
| self.assertEqual(test_span.t, db_span.t) | ||
| self.assertEqual(db_span.p, test_span.s) | ||
|
|
||
| self.assertEqual(None, db_span.ec) |
| self.assertEqual(test_span.t, db_span.t) | ||
| self.assertEqual(db_span.p, test_span.s) | ||
|
|
||
| self.assertEqual(None, db_span.ec) |
| self.assertEqual(test_span.t, db_span.t) | ||
| self.assertEqual(db_span.p, test_span.s) | ||
|
|
||
| self.assertEqual(None, db_span.ec) |
| self.assertEqual(test_span.t, db_span.t) | ||
| self.assertEqual(db_span.p, test_span.s) | ||
|
|
||
| self.assertEqual(None, db_span.ec) |
| self.assertEqual(test_span.t, db_span.t) | ||
| self.assertEqual(db_span.p, test_span.s) | ||
|
|
||
| self.assertEqual(None, db_span.ec) |
| self.assertEqual(test_span.t, db_span.t) | ||
| self.assertEqual(db_span.p, test_span.s) | ||
|
|
||
| self.assertEqual(None, db_span.ec) |
| self.assertEqual(test_span.t, db_span.t) | ||
| self.assertEqual(db_span.p, test_span.s) | ||
|
|
||
| self.assertEqual(None, db_span.ec) |
| self.assertEqual(test_span.t, db_span.t) | ||
| self.assertEqual(db_span.p, test_span.s) | ||
|
|
||
| self.assertEqual(None, db_span.ec) |
| result = cursor.execute("""SELECT * from users""") | ||
| cursor.fetchone() | ||
|
|
||
| assert(result >= 0) |
There was a problem hiding this comment.
This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.
| result = cursor.execute("""SELECT * from users""") | ||
| cursor.fetchone() | ||
|
|
||
| assert(result >= 0) |
There was a problem hiding this comment.
This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.
| cursor.fetchone() | ||
| self.db.close() | ||
|
|
||
| assert(result >= 0) |
There was a problem hiding this comment.
This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.
| result = cursor.execute("""SELECT * from users""") | ||
| cursor.fetchone() | ||
|
|
||
| assert(result >= 0) |
There was a problem hiding this comment.
This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.
| cursor.fetchone() | ||
| self.db.close() | ||
|
|
||
| assert(result >= 0) |
There was a problem hiding this comment.
This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.
| result = cursor.execute("""SELECT * from users""") | ||
| cursor.fetchone() | ||
|
|
||
| assert(result >= 0) |
There was a problem hiding this comment.
This assert is for catching runtime anomalies in the production code, after it is deployed.
For unit testing we should use assertTrue, if nothing else fits better.
| self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port']) | ||
|
|
||
| def test_connect_ctx_mngr(self): | ||
| result = None |
There was a problem hiding this comment.
The context manager does not create a new scope, the result = cursor.execute( statement is enough. Please remove this line, luckily this is not Pascal or ANSI C, where you have to declare the variables way ahead of assigning them.
| self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port']) | ||
|
|
||
| def test_cursor_ctx_mngr(self): | ||
| result = None |
| self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port']) | ||
|
|
||
| def test_connect_cursor_ctx_mngr(self): | ||
| result = None |
| self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port']) | ||
|
|
||
| def test_connect_ctx_mngr(self): | ||
| result = None |
| self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port']) | ||
|
|
||
| def test_cursor_ctx_mngr(self): | ||
| result = None |
| self.assertEqual(db_span.data["mysql"]["port"], testenv['mysql_port']) | ||
|
|
||
| def test_connect_cursor_ctx_mngr(self): | ||
| result = None |
Ferenc-
left a comment
There was a problem hiding this comment.
This is just a preliminary review. With some minor formalities commented for now.
Add the test_connect_ctx_mngr UT to test the span creation when using the psycopg2 connect with context manager, and the test_connect_cursor_ctx_mngr UT when using the psycopg2 connect and cursos context manager.